package com.brand.prisd.fragment;


import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.brand.prisd.R;
import com.brand.prisd.ViewHolder.Type8ViewHolder;
import com.brand.prisd.ViewHolder.Type9ViewHolder;
import com.brand.prisd.activity.ChooseDeviceTypeActivity;

import java.util.Objects;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends Fragment {
    @BindView(R.id.rv)
    RecyclerView rv;
    Unbinder unbinder;
    @BindView(R.id.tv_PRISD)
    TextView tvPRISD;
    @BindView(R.id.iv_down_arrow)
    ImageView ivDownArrow;
    @BindView(R.id.iv_voice)
    ImageView ivVoice;
    @BindView(R.id.iv_add)
    ImageView ivAdd;
    private View view;
    private Context context;


    public HomeFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_home, container, false);
        context = view.getContext();
        unbinder = ButterKnife.bind(this, view);

        initView();

        return view;
    }

    private void initView() {
        rv.setLayoutManager(new LinearLayoutManager(getContext()));
        rv.setAdapter(new RVAdapter());

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @TargetApi(Build.VERSION_CODES.KITKAT)
    @OnClick({R.id.tv_PRISD, R.id.iv_down_arrow, R.id.iv_voice, R.id.iv_add})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_PRISD:
            case R.id.iv_down_arrow:
            case R.id.iv_voice:
                Toast.makeText(getContext(), "待开发...", Toast.LENGTH_SHORT).show();
                break;
            case R.id.iv_add:
                startActivity(new Intent(context,ChooseDeviceTypeActivity.class));
                Objects.requireNonNull(getActivity()).finish();
                break;
        }
    }


    class RVAdapter extends RecyclerView.Adapter {

        private static final int ITEM_VIEW_TYPE_8 = 8;
        private static final int ITEM_VIEW_TYPE_9 = 9;


        @Override
        public int getItemViewType(int position) {
            switch (position) {
                case 0:
                    return ITEM_VIEW_TYPE_8;
                default:
                    return ITEM_VIEW_TYPE_9;
            }

        }

        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
            switch (viewType) {
                case ITEM_VIEW_TYPE_8:
                    return new Type8ViewHolder(LayoutInflater.from(getContext()).inflate(R.layout.layout_weather, viewGroup, false));
                default:
                    return new Type9ViewHolder(LayoutInflater.from(getContext()).inflate(R.layout.layout_viewpager_tablayout, viewGroup, false));
            }

        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
            if (viewHolder instanceof Type8ViewHolder) {
                Type8ViewHolder type8ViewHolder = (Type8ViewHolder) viewHolder;
            } else if (viewHolder instanceof Type9ViewHolder) {
                Type9ViewHolder type9ViewHolder = (Type9ViewHolder) viewHolder;
                type9ViewHolder.getFragmentManger(HomeFragment.this.getChildFragmentManager());
            }
        }

        @Override
        public int getItemCount() {
            return 2;
        }
    }
}
